Conditions | 9 |
Total Lines | 94 |
Code Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import React, { Component } from 'react'; |
||
29 | |||
30 | componentDidMount() { |
||
31 | console.log(this.props); |
||
32 | this._isMounted = true; |
||
33 | // fetch realtime data at the first time |
||
34 | let isResponseOK = false; |
||
35 | if (this.props.distributionSystemID != undefined) { |
||
36 | fetch(APIBaseURL + '/reports/distributionsystem?distributionsystemid=' + this.props.distributionSystemID, { |
||
37 | method: 'GET', |
||
38 | headers: { |
||
39 | "Content-type": "application/json", |
||
40 | "User-UUID": getCookieValue('user_uuid'), |
||
41 | "Token": getCookieValue('token') |
||
42 | }, |
||
43 | body: null, |
||
44 | |||
45 | }).then(response => { |
||
46 | if (response.ok) { |
||
47 | isResponseOK = true; |
||
48 | } |
||
49 | return response.json(); |
||
50 | }).then(json => { |
||
51 | if (isResponseOK) { |
||
52 | console.log(json); |
||
53 | let pointList = []; |
||
54 | json.forEach((currentCircuit, circuitIndex) => { |
||
55 | json[circuitIndex]['points'].forEach((currentPoint, pointIndex) => { |
||
56 | let pointItem = {} |
||
57 | pointItem['circuit'] = currentCircuit['name']; |
||
58 | pointItem['point'] = currentPoint['name']; |
||
59 | pointItem['value'] = currentPoint['value']; |
||
60 | pointItem['units'] = currentPoint['units']; |
||
61 | pointList.push(pointItem); |
||
62 | }); |
||
63 | }); |
||
64 | if (this._isMounted) { |
||
65 | this.setState({ |
||
66 | pointList: pointList, |
||
67 | }); |
||
68 | } |
||
69 | } else { |
||
70 | toast.error(json.description) |
||
71 | } |
||
72 | }).catch(err => { |
||
73 | console.log(err); |
||
74 | }); |
||
75 | }; |
||
76 | |||
77 | //fetch realtime data at regular intervals |
||
78 | this.refreshInterval = setInterval(() => { |
||
79 | let isResponseOK = false; |
||
80 | if (this.props.distributionSystemID != undefined) { |
||
81 | fetch(APIBaseURL + '/reports/distributionsystem?distributionsystemid=' + this.props.distributionSystemID, { |
||
82 | method: 'GET', |
||
83 | headers: { |
||
84 | "Content-type": "application/json", |
||
85 | "User-UUID": getCookieValue('user_uuid'), |
||
86 | "Token": getCookieValue('token') |
||
87 | }, |
||
88 | body: null, |
||
89 | |||
90 | }).then(response => { |
||
91 | if (response.ok) { |
||
92 | isResponseOK = true; |
||
93 | } |
||
94 | return response.json(); |
||
95 | }).then(json => { |
||
96 | if (isResponseOK) { |
||
97 | console.log(json); |
||
98 | let pointList = []; |
||
99 | json.forEach((currentCircuit, circuitIndex) => { |
||
100 | json[circuitIndex]['points'].forEach((currentPoint, pointIndex) => { |
||
101 | let pointItem = {} |
||
102 | pointItem['circuit'] = currentCircuit['name']; |
||
103 | pointItem['point'] = currentPoint['name']; |
||
104 | pointItem['value'] = currentPoint['value']; |
||
105 | pointItem['units'] = currentPoint['units']; |
||
106 | pointList.push(pointItem); |
||
107 | }); |
||
108 | }); |
||
109 | |||
110 | if (this._isMounted) { |
||
111 | this.setState({ |
||
112 | pointList: pointList, |
||
113 | }); |
||
114 | } |
||
115 | } else { |
||
116 | toast.error(json.description) |
||
117 | } |
||
118 | }).catch(err => { |
||
119 | console.log(err); |
||
120 | }); |
||
121 | } |
||
122 | }, (60 + Math.floor(Math.random() * Math.floor(10))) * 1000); // use random interval to avoid paralels requests |
||
123 | } |
||
165 |